package com.seezoon.application.scheduler;

import com.seezoon.domain.service.DeviceInfoService;
import com.seezoon.infrastructure.properties.AppProperties;
import java.time.Instant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 设备状态检查
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class DeviceStatusTask {

    /**
     * 一分钟一次
     */
    private final long interval = 60 * 1000;
    private final AppProperties appProperties;
    private final DeviceInfoService deviceInfoService;

    @Scheduled(fixedDelay = interval)
    public void execute() {
        try {
            long heartbeatTimeout = appProperties.getDevice().getHeartbeatTimeout();
            int count = deviceInfoService.offline(Instant.now().minusSeconds(heartbeatTimeout));
            log.info("offline device count:{}", count);
        } catch (Throwable e) {
            log.error("task error", e);
        }
    }
}
